package com.itheima.mm.service.impl;
import com.itheima.mm.base.BaseService;
import com.itheima.mm.common.QuestionConst;
import com.itheima.mm.dao.*;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.exception.MmException;
import com.itheima.mm.pojo.*;
import com.itheima.mm.service.QuestionService;
import com.itheima.mm.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author ：seanyang
 * @date ：Created in 2019/8/12
 * @description ：题目业务实现类
 * @version: 1.0
 */
@Service
@Slf4j
public class QuestionServiceImpl extends BaseService implements QuestionService {
	@Autowired
	private QuestionDao questionDao;

	@Autowired
	private ReviewLogDao reiviewLogDao;

	@Autowired
	private CompanyDao companyDao;

	@Autowired
	private IndustryDao industryDao;

	@Autowired
	private TagDao tagDao;

	@Autowired
	private QuestionItemDao questionItemDao;
	@Override
	public PageResult findByPage(QueryPageBean queryPageBean) {
		log.info("QuestionService findByPage:{}",queryPageBean);
		if(queryPageBean.getQueryParams() == null){
			queryPageBean.setQueryParams(new HashMap());
		}
		queryPageBean.getQueryParams().put("isClassic",0);
		// 获取数据集
		List<Question> questionList = questionDao.selectIsClassicByPage(queryPageBean);
		// 获取数据记录格式
		Long totalCount = questionDao.selectCountIsClassicByPage(queryPageBean);
		return new PageResult(totalCount,questionList);
	}

	/**
	 * 更新题目信息
	 * 更新选项信息（更新旧选项，添加信息选项）
	 * 更新标签信息 （添加新标签，删除旧关系，插入新关系）
	 */

	@Override
	public PageResult findClassicByPage(QueryPageBean queryPageBean) {
		log.info("QuestionService findClassicByPage:{}",queryPageBean);
		if(queryPageBean.getQueryParams() == null){
			queryPageBean.setQueryParams(new HashMap());
		}
		// 查询精选题目
		queryPageBean.getQueryParams().put("isClassic",1);
		List<Question> questionList = questionDao.selectIsClassicByPage(queryPageBean);
		// 获取每条记录的审核状态
		for (Question question:questionList){
			ReviewLog reviewLog = reiviewLogDao.selectLastByQuestionId(question.getId());
			if (reviewLog == null){
				reviewLog = new ReviewLog();
				reviewLog.setStatus(0);
				reviewLog.setComments("");
				reviewLog.setReviewer("");
				reviewLog.setCreateDate("");
			}
			question.setReviewLog(reviewLog);
		}
		// 获取总记录数
		Long totalCount = questionDao.selectCountIsClassicByPage(queryPageBean);
		return new PageResult(totalCount,questionList);
	}

	@Transactional
	@Override
	public void updateIsClassic(Integer questionId, Integer isClassic) {
		 questionDao.updateIsClassic(questionId,isClassic);

	}

	@Override
	public void addQuertion(Question question) {
		questionDao.addQuestion( question);
		int quertionid=question.getId();
		for (QuestionItem questionItem : question.getQuestionItemList()) {
			//questionItem.setQuestionId(questionDao.findQuestionIdBySubject(question.getSubject()));
			questionItem.setQuestionId(quertionid);
			questionDao.addQuestionItemList(questionItem);
		}
		for (Tag tag : question.getTagList()) {
			questionDao.addQuestionTag(quertionid,tag.getId());
		}
	}

	@Override
	public Question findQuestionById(Integer id) {
		log.info("QuestionService findQuestionById:{}",id);
		// 获取题目基本信息
		// 级联获取题目选项信息
		Question question = questionDao.selectById(id);
		log.debug("question:{}",question);
		// 获取所属标签名称列表
		initQuestionTag(tagDao,question);
		// 根据公司ID，获取所属公司信息
		initQuestionCompany(companyDao,question);
		return question;
	}

	// 获取题目公司及行业信息
	private void initQuestionCompany(CompanyDao companyDao, Question question){
		log.info("QuestionService initQuestionCompany:{}",question.getCompanyId());
		Company company = companyDao.selectByIdForQuestion(question.getCompanyId());
		// 根据公司ID，获取所属行业信息
		List<Industry> industryList = industryDao.selectIndustryListByCompany(question.getCompanyId());
		List<String> industryNameList = new ArrayList<>();
		for (Industry one:industryList){
			industryNameList.add(one.getName());
		}
		company.setIndustryNameList(industryNameList);
		company.setIndustryList(industryList);
		// 设置所属公司(包含所属城市行业)
		question.setCompany(company);
	}

	// 获取题目标签信息
	private void initQuestionTag(TagDao tagDao, Question question){
		log.info("QuestionService initQuestionTag:{}",question.getId());
		List<Tag> tagList = tagDao.selectTagListByQuestionId(question.getId());
		List<String> tagNameList = new ArrayList<>();
		for (Tag one:tagList){
			tagNameList.add(one.getName());
		}
		// 设置标签名称列表
		question.setTagNameList(tagNameList);
		question.setTagList(tagList);
	}


	// 添加题目信息

	// 更新题目选项信息

	// 更新标签信息




	@Transactional
	@Override
	public void updateStatus(Integer questionId, Integer status) {
		log.info("QuestionService updateStatus:questionId-{},status-{}",questionId,status);
		questionDao.updateStatus(questionId,status);
	}
}
